package cn.mesmile.code.sorting;

import java.util.Arrays;

/**
 * @author zb
 * @date 2021/11/29 9:34
 * @Description
 */
public class BubbleSort {

    public static void sorted(int[] nums){
        int length = nums.length;
        boolean isSorted = false;
        for (int i = 0; i < length - 1 && !isSorted; i++) {
            // 若后面都是排好序的，则可以提前结束
            isSorted = true;
            for (int j = 0; j < length - 1 - i; j++) {
                if (nums[j] > nums[j + 1]) {
                    int t = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = t;
                    isSorted = false;
                }
            }
        }
    }

    public static void main(String[] args) {
//        int[] nums = new int[10];
        int[] nums = {1,9,3,8,6,5,4,7,2};
        sorted(nums);
        System.out.println("nums.toString() = " + Arrays.toString(nums));
    }


}
